<?php


use app\common\model\Slide;
use think\response\Json;
use app\common\model\SysConfig;
use tools\Crypt;

if(!function_exists('success')){
    /**
     * 操作成功
     * @param string $msg
     * @param string|array $data
     * @param int $code
     * @return Json
     */
    function success( $data = '', string $msg = 'success', int $code = 200): Json
    {
        return result($msg, $data, $code);
    }
}

if(!function_exists('error')){
    /**
     * 操作失败
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function error(string $msg = 'fail', string $data = '', int $code = 500): Json
    {
        return result($msg, $data, $code);
    }
}

if(!function_exists('result')){
    /**
     * 返回json结果
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function result(string $msg = 'fail', string $data = '', int $code = 500): Json
    {
        $header = [];
        //处理跨域请求问题
        if (config('api.cross_domain.allow')) {
            $header = ['Access-Control-Allow-Origin' => '*'];
            if (request()->isOptions()) {
                $header = config('api.cross_domain.header');
                return json('',200,$header);
            }
        }

        return json([
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        ], $code, $header);
    }
}

if(!function_exists('unauthorized')){
    /**
     * 未授权
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function unauthorized(string $msg = 'unauthorized', string $data = '', int $code = 401): Json
    {
        return result($msg, $data, $code);
    }
}

if(!function_exists('client_error')){
    /**
     * 客户端错误
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function client_error(string $msg = 'client error', string $data = '', int $code = 400): Json
    {
        return result($msg, $data, $code);
    }
}

if(!function_exists('server_error')){
    /**
     * 服务端错误
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function server_error(string $msg = 'server error', string $data = '', int $code = 500): Json
    {
        return result($msg, $data, $code);
    }
}

if(!function_exists('error_404')){
    /**
     * 资源或接口不存在
     * @param string $msg
     * @param string $data
     * @param int $code
     * @return Json
     */
    function error_404(string $msg = '404 not found', string $data = '', int $code = 404): Json
    {
        return result($msg, $data, $code);
    }
}
function get_ad($alias, $num = '999'): array
{
    $_malladviceModel = new Slide();
    $where = 'alias="'.$alias.'"';
    $malladvice_list = $_malladviceModel->where($where)->limit($num)->order('sort_number desc')->select();
    $ad = array();
    $web_url = SysConfig::get('website', 'img_url');
    foreach ($malladvice_list as $key => $row) {
        $ad[$key]['id'] = $row['id'];
        if($row['jump_type']==1){
            $ad[$key]['unionid'] = Crypt::encrypt($row['jump_target']);
        }else{
            $ad[$key]['url'] = $row['jump_target'];
        }
        $ad[$key]['type'] = $row['jump_type'];
        $ad[$key]['img'] = $web_url . $row['img'];
    }
    return $ad;
}




